#include "ChartWidget.h"

#include <QDebug>
#include <QPainter>
#include <QSqlError>
#include <QSqlQuery>
#include <limits>

#include "../../common/ConstData.h"
#include "../../sql/database.h"

// CustomChartView 实现
CustomChartView::CustomChartView(QChart *chart, QWidget *parent)
    : QChartView(chart, parent),
      crosshairEnabled(false),  // 默认不激活
      crosshairVisible(false),
      dataPoints(nullptr),
      series(nullptr),
      lastNearestPoint(QPointF())
{
    setMouseTracking(true);
    setRubberBand(QChartView::NoRubberBand);
}

void CustomChartView::mouseMoveEvent(QMouseEvent *event)
{
    if (crosshairEnabled && dataPoints && series && !dataPoints->isEmpty()) {
        updateCrosshair(event->pos());

        // 使用十字光标的X轴位置（屏幕坐标）来寻找数据
        // 创建一个虚拟的图表点，X轴使用十字光标的X位置，Y轴使用任意值
        QPointF crosshairScreenPoint(crosshairPosition.x(), event->pos().y());
        QPointF crosshairChartPoint = chart()->mapToValue(crosshairScreenPoint, series);

        // 根据十字光标的X轴位置（时间）找到最接近的实际数据点
        QPointF nearestDataPoint = findNearestDataPointByX(crosshairChartPoint.x());

        // 只有当最近点发生变化时才发出信号，减少不必要的更新
        if (nearestDataPoint != lastNearestPoint) {
            lastNearestPoint = nearestDataPoint;
            emit crosshairMoved(crosshairChartPoint, nearestDataPoint);
        }
    }
    QChartView::mouseMoveEvent(event);
}

void CustomChartView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        // 切换十字光标激活状态
        crosshairEnabled = !crosshairEnabled;

        if (!crosshairEnabled) {
            // 取消激活时清理显示
            crosshairVisible = false;
            lastNearestPoint = QPointF();
            update();
            emit crosshairMoved(QPointF(), QPointF());  // 清理数据显示
        } else {
            // 激活时立即显示当前位置的十字光标
            if (dataPoints && series && !dataPoints->isEmpty()) {
                updateCrosshair(event->pos());

                QPointF crosshairScreenPoint(crosshairPosition.x(), event->pos().y());
                QPointF crosshairChartPoint = chart()->mapToValue(crosshairScreenPoint, series);
                QPointF nearestDataPoint = findNearestDataPointByX(crosshairChartPoint.x());

                if (nearestDataPoint != lastNearestPoint) {
                    lastNearestPoint = nearestDataPoint;
                    emit crosshairMoved(crosshairChartPoint, nearestDataPoint);
                }
            }
        }
    }
    QChartView::mousePressEvent(event);
}

void CustomChartView::leaveEvent(QEvent *event)
{
    if (crosshairEnabled) {
        crosshairVisible = false;
        lastNearestPoint = QPointF();  // 清理缓存
        update();
        // 发出信号清理数据显示
        emit crosshairMoved(QPointF(), QPointF());
    }
    QChartView::leaveEvent(event);
}

void CustomChartView::paintEvent(QPaintEvent *event)
{
    QChartView::paintEvent(event);

    if (crosshairEnabled && crosshairVisible) {
        QPainter painter(viewport());
        painter.setPen(QPen(QColor(255, 255, 255, 150), 1, Qt::DashLine));

        // 绘制十字线
        QRect plotArea = chart()->plotArea().toRect();
        if (plotArea.contains(crosshairPosition)) {
            // 水平线
            painter.drawLine(plotArea.left(), crosshairPosition.y(), plotArea.right(),
                             crosshairPosition.y());
            // 垂直线
            painter.drawLine(crosshairPosition.x(), plotArea.top(), crosshairPosition.x(),
                             plotArea.bottom());
        }
    }
}

void CustomChartView::updateCrosshair(const QPoint &position)
{
    crosshairPosition = position;
    crosshairVisible = true;
    update();
}

QPointF CustomChartView::findNearestDataPointByX(double xValue)
{
    if (!dataPoints || dataPoints->isEmpty()) {
        return QPointF();
    }

    QPointF nearestPoint;
    double minXDistance = std::numeric_limits<double>::max();

    // 只基于X轴值（时间）找最接近的实际数据点
    for (const QPointF &actualPoint : *dataPoints) {
        double xDistance = qAbs(actualPoint.x() - xValue);

        if (xDistance < minXDistance) {
            minXDistance = xDistance;
            nearestPoint = actualPoint;
        }
    }

    return nearestPoint;
}

ChartWidget::ChartWidget(QWidget *parent, ChartDisplayMode mode, const QString &deviceId)
    : QWidget(parent),
      mainLayout(nullptr),
      controlLayout(nullptr),
      deviceLabel(nullptr),
      deviceComboBox(nullptr),
      backButton(nullptr),
      previousButton(nullptr),
      nextButton(nullptr),
      zoomInButton(nullptr),
      zoomOutButton(nullptr),
      timeRangeLabel(nullptr),
      chart(nullptr),
      chartView(nullptr),
      series(nullptr),
      axisY(nullptr),
      axisX(nullptr),
      m_displayMode(mode),
      m_fixedDeviceId(deviceId),
      displayHours(24)  // 默认显示24小时
{
    setupUI();

    // 根据显示模式进行不同的初始化
    if (m_displayMode == ChartDisplayMode::Window) {
        setWindowTitle(QString("设备数据图表 - %1").arg(m_fixedDeviceId));
        setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
        setWindowModality(Qt::ApplicationModal);
        setFixedSize(1600, 1000);
        // 窗口模式：隐藏设备选择相关控件和返回按钮
        if (deviceLabel) deviceLabel->setVisible(false);
        if (deviceComboBox) deviceComboBox->setVisible(false);
        if (backButton) backButton->setVisible(false);

        // 直接加载指定设备的数据
        if (!m_fixedDeviceId.isEmpty()) {
            currentDeviceName = m_fixedDeviceId;
            loadChartData(m_fixedDeviceId);
        }
    } else {
        // 嵌入模式：显示所有控件，加载设备列表
        loadDeviceNames();
    }
}

ChartWidget::~ChartWidget() {}

void ChartWidget::setupUI()
{
    // 作为内嵌组件，不设置窗口标题和固定大小
    // 创建主布局
    mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 0);  // 设置主布局边距

    mainLayout->setSpacing(6);  // 设置布局间距

    // 创建控制面板布局
    controlLayout = new QHBoxLayout();
    controlLayout->setContentsMargins(10, 0, 10, 0);  // 设置控制布局边距为0

    // 设备选择标签
    deviceLabel = new QLabel("选择设备:", this);
    deviceLabel->setMinimumWidth(80);

    // 设备选择下拉框
    deviceComboBox = new QComboBox(this);
    deviceComboBox->setMinimumWidth(150);
    connect(deviceComboBox, &QComboBox::currentTextChanged, this,
            &ChartWidget::onDeviceSelectionChanged);

    // 返回按钮
    backButton = new QPushButton("返回表格", this);
    connect(backButton, &QPushButton::clicked, this, &ChartWidget::onBackButtonClicked);

    // 导航按钮
    previousButton = new QPushButton("前移", this);
    previousButton->setMaximumWidth(80);
    connect(previousButton, &QPushButton::clicked, this, &ChartWidget::onPreviousClicked);

    nextButton = new QPushButton("后移", this);
    nextButton->setMaximumWidth(80);
    connect(nextButton, &QPushButton::clicked, this, &ChartWidget::onNextClicked);

    zoomInButton = new QPushButton("放大", this);
    zoomInButton->setMaximumWidth(80);
    connect(zoomInButton, &QPushButton::clicked, this, &ChartWidget::onZoomInClicked);

    zoomOutButton = new QPushButton("缩小", this);
    zoomOutButton->setMaximumWidth(80);
    connect(zoomOutButton, &QPushButton::clicked, this, &ChartWidget::onZoomOutClicked);

    // 时间范围标签
    timeRangeLabel = new QLabel("时间范围: --", this);
    timeRangeLabel->setMinimumWidth(200);

    // 数据信息显示标签
    dataInfoLabel = new QLabel("数据信息: 点击图表激活十字光标", this);
    dataInfoLabel->setMinimumWidth(250);
    dataInfoLabel->setStyleSheet("QLabel { color: #13b554; }");

    controlLayout->addWidget(deviceLabel);
    controlLayout->addWidget(deviceComboBox);
    controlLayout->addWidget(previousButton);
    controlLayout->addWidget(nextButton);
    controlLayout->addWidget(zoomInButton);
    controlLayout->addWidget(zoomOutButton);
    controlLayout->addWidget(timeRangeLabel);
    controlLayout->addWidget(dataInfoLabel);
    controlLayout->addStretch();
    controlLayout->addWidget(backButton);  // 返回按钮放在右侧

    // 创建图表
    chart = new QChart();
    chart->setTitle("设备数据趋势图");
    chart->setAnimationOptions(QChart::SeriesAnimations);

    // 根据当前主题应用图表颜色
    applyThemeColors();

    // 创建图表视图
    chartView = new CustomChartView(chart, this);
    chartView->setRenderHint(QPainter::Antialiasing);

    // 连接十字光标信号
    connect(chartView, &CustomChartView::crosshairMoved, this, &ChartWidget::onCrosshairMoved);

    // 创建坐标轴
    axisX = new QDateTimeAxis();
    axisX->setFormat("hh:mm");  // 简化格式，只显示时分
    axisX->setTickCount(6);     // 设置时间轴刻度数量

    axisY = new QValueAxis();

    // 坐标轴颜色在 applyThemeColors() 中设置

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);

    // 设置轴的字体大小，确保标签可见
    QFont axisFont;
    axisFont.setPointSize(10);
    axisX->setLabelsFont(axisFont);
    axisY->setLabelsFont(axisFont);

    // 强制显示标签
    axisX->setLabelsVisible(true);
    axisY->setLabelsVisible(true);

    // 添加到主布局
    mainLayout->addLayout(controlLayout);
    mainLayout->addWidget(chartView);

    setLayout(mainLayout);
}

void ChartWidget::loadDeviceNames()
{
    deviceComboBox->clear();
    deviceComboBox->addItem("请选择设备");

    // 从数据库获取所有设备名称
    QSqlQuery query;
    query.prepare(
        "SELECT DISTINCT device_name FROM device_readings WHERE device_name IS NOT NULL AND "
        "device_name != '' ORDER BY device_name");

    if (query.exec()) {
        while (query.next()) {
            QString deviceName = query.value("device_name").toString();
            if (!deviceName.isEmpty()) {
                deviceComboBox->addItem(deviceName);
            }
        }
    } else {
        qDebug() << "加载设备名称失败:" << query.lastError().text();
    }
}

void ChartWidget::onDeviceSelectionChanged()
{
    QString selectedDevice = deviceComboBox->currentText();
    if (selectedDevice != "请选择设备") {
        updateChart();
    }
}

void ChartWidget::updateChart()
{
    QString selectedDevice = deviceComboBox->currentText();
    if (selectedDevice == "请选择设备") {
        return;
    }

    loadChartData(selectedDevice);
}

void ChartWidget::loadChartData(const QString &deviceName)
{
    currentDeviceName = deviceName;
    allDataPoints.clear();

    // 从数据库查询最近30天的数据
    QSqlQuery query;

    // 先获取最后一条记录的时间
    QSqlQuery lastQuery;
    lastQuery.prepare("SELECT MAX(timestamp) FROM device_readings WHERE device_name = ?");
    lastQuery.addBindValue(deviceName);

    QString endTime = "datetime('now')";  // 默认使用当前时间
    if (lastQuery.exec() && lastQuery.next()) {
        QVariant lastTimestamp = lastQuery.value(0);
        if (!lastTimestamp.isNull()) {
            endTime = QString("'%1'").arg(lastTimestamp.toString());
        }
    }

    query.prepare(QString("SELECT timestamp, value FROM device_readings "
                          "WHERE device_name = ? AND timestamp >= datetime(%1, '-30 days') "
                          "ORDER BY timestamp ASC")
                      .arg(endTime));
    query.addBindValue(deviceName);

    if (query.exec()) {
        QDateTime firstTime, lastTime;
        bool firstPoint = true;

        while (query.next()) {
            QString timestampStr = query.value("timestamp").toString();
            double value = query.value("value").toDouble();

            QDateTime timestamp = QDateTime::fromString(timestampStr, "yyyy-MM-dd hh:mm:ss");
            if (timestamp.isValid()) {
                allDataPoints.append(QPointF(timestamp.toMSecsSinceEpoch(), value));

                if (firstPoint) {
                    firstTime = lastTime = timestamp;
                    firstPoint = false;
                } else {
                    if (timestamp < firstTime) firstTime = timestamp;
                    if (timestamp > lastTime) lastTime = timestamp;
                }
            }
        }

        if (!allDataPoints.isEmpty()) {
            dataStartTime = firstTime;
            dataEndTime = lastTime;

            // 设置当前显示范围为最近的数据，对齐到整点
            currentEndTime = QDateTime(dataEndTime.date(), QTime(dataEndTime.time().hour(), 0, 0));
            if (currentEndTime < dataEndTime) {
                currentEndTime = currentEndTime.addSecs(3600);  // 向上取整到下一个整点
            }
            currentStartTime = currentEndTime.addSecs(-(displayHours) * 3600);

            updateTimeRange();
        } else {
            chart->setTitle(QString("%1 - 暂无数据").arg(deviceName));
        }
    } else {
        qDebug() << "查询设备数据失败:" << query.lastError().text();
        chart->setTitle(QString("%1 - 数据加载失败").arg(deviceName));
    }
}

void ChartWidget::updateTimeRange()
{
    if (allDataPoints.isEmpty()) {
        return;
    }

    // 清除之前的数据
    if (series) {
        if (chart->series().contains(series)) {
            chart->removeSeries(series);
        }
        delete series;
        series = nullptr;
    }

    // 创建新的数据系列
    series = new QLineSeries();
    series->setName(currentDeviceName + " 数据趋势");

    // 设置线条颜色和样式
    QPen pen(QColor("#4FC3F7"));
    pen.setWidth(2);
    series->setPen(pen);

    // 筛选当前时间范围内的数据点
    qint64 startMSecs = currentStartTime.toMSecsSinceEpoch();
    qint64 endMSecs = currentEndTime.toMSecsSinceEpoch();

    visibleDataPoints.clear();  // 清除可见数据点列表
    double minValue = std::numeric_limits<double>::max();
    double maxValue = std::numeric_limits<double>::lowest();
    int visibleCount = 0;

    for (const QPointF &point : allDataPoints) {
        qint64 pointTime = point.x();
        if (pointTime >= startMSecs && pointTime <= endMSecs) {
            series->append(point);
            visibleDataPoints.append(point);  // 记录实际数据点
            double value = point.y();
            if (value < minValue) minValue = value;
            if (value > maxValue) maxValue = value;
            visibleCount++;
        }
    }

    if (visibleCount > 0) {
        // 添加数据系列到图表
        chart->addSeries(series);
        series->attachAxis(axisX);
        series->attachAxis(axisY);

        // 设置十字光标数据
        chartView->setDataPoints(&visibleDataPoints);
        chartView->setLineSeries(series);

        // 设置时间轴范围
        axisX->setRange(currentStartTime, currentEndTime);

        // 根据显示时长调整时间格式和刻度
        if (displayHours <= 24) {  // 1天
            axisX->setFormat("hh:mm");
            axisX->setTickCount(25);
        } else if (displayHours <= 72) {  // 3天
            axisX->setFormat("MM-dd hh:mm");
            axisX->setTickCount(4);
        } else if (displayHours <= 168) {  // 7天
            axisX->setFormat("MM-dd hh:mm");
            axisX->setTickCount(8);
        } else {  // 30天
            axisX->setFormat("MM-dd hh:mm");
            axisX->setTickCount(16);
        }

        // 强制刷新图表和坐标轴
        axisX->setVisible(true);
        axisX->setLabelsVisible(true);

        // 尝试重新设置字体
        QFont axisFont;
        axisFont.setPointSize(10);
        axisX->setLabelsFont(axisFont);

        // 重新应用主题颜色
        applyThemeColors();

        // 强制更新图表
        chart->update();
        chartView->update();

        // 设置Y轴范围
        double valueRange = maxValue - minValue;
        double margin = valueRange * 0.1;
        if (margin == 0) margin = 1;
        axisY->setRange(minValue - margin, maxValue + margin);

        // 更新标题和标签
        chart->setTitle(QString("%1 数据趋势图 (显示 %2/%3 个数据点)")
                            .arg(currentDeviceName)
                            .arg(visibleCount)
                            .arg(allDataPoints.size()));

        timeRangeLabel->setText(QString("时间范围: %1 到 %2 (%3小时)")
                                    .arg(currentStartTime.toString("MM-dd hh:mm"))
                                    .arg(currentEndTime.toString("MM-dd hh:mm"))
                                    .arg(displayHours));
    } else {
        chart->setTitle(QString("%1 - 当前时间范围无数据").arg(currentDeviceName));
        timeRangeLabel->setText("时间范围: 无数据");
    }
}

void ChartWidget::onPreviousClicked()
{
    if (allDataPoints.isEmpty()) return;

    // 向前移动显示时长，保持整点对齐
    int moveHours = displayHours;
    if (moveHours == 0) moveHours = 1;  // 至少移动1小时

    QDateTime newEndTime = currentEndTime.addSecs(-moveHours * 3600);
    QDateTime newStartTime = newEndTime.addSecs(-displayHours * 3600);

    // 确保不超出数据范围
    if (newStartTime < dataStartTime) {
        newStartTime = dataStartTime;
        newEndTime = newStartTime.addSecs(displayHours * 3600);
    }

    currentStartTime = newStartTime;
    currentEndTime = newEndTime;
    updateTimeRange();
}

void ChartWidget::onNextClicked()
{
    if (allDataPoints.isEmpty()) return;

    // 向后移动显示时长，保持整点对齐
    int moveHours = displayHours;
    if (moveHours == 0) moveHours = 1;  // 至少移动1小时

    QDateTime newStartTime = currentStartTime.addSecs(moveHours * 3600);
    QDateTime newEndTime = currentEndTime.addSecs(moveHours * 3600);

    // 确保不超出数据范围
    if (newEndTime > dataEndTime) {
        newEndTime = dataEndTime;
        newStartTime = newEndTime.addSecs(-displayHours * 3600);
    }

    currentStartTime = newStartTime;
    currentEndTime = newEndTime;
    updateTimeRange();
}

void ChartWidget::onZoomInClicked()
{
    if (allDataPoints.isEmpty()) return;

    // 放大：按固定档位减少显示时长
    int newDisplayHours = displayHours;
    if (displayHours >= 720) {  // 30天 -> 7天
        newDisplayHours = 168;
    } else if (displayHours >= 168) {  // 7天 -> 3天
        newDisplayHours = 72;
    } else if (displayHours >= 72) {  // 3天 -> 1天
        newDisplayHours = 24;
    } else if (displayHours > 24) {  // 其他值 -> 1天
        newDisplayHours = 24;
    } else {
        return;  // 已经是最小档位（1天），不能再放大
    }

    displayHours = newDisplayHours;

    // 重新计算时间范围，保持中心点
    QDateTime centerTime = currentStartTime.addSecs(
        (currentEndTime.toSecsSinceEpoch() - currentStartTime.toSecsSinceEpoch()) / 2);
    currentStartTime = centerTime.addSecs(-(displayHours) * 3600 / 2);
    currentEndTime = centerTime.addSecs((displayHours) * 3600 / 2);

    updateTimeRange();
}

void ChartWidget::onZoomOutClicked()
{
    if (allDataPoints.isEmpty()) return;

    // 缩小：按固定档位增加显示时长
    int newDisplayHours = displayHours;
    if (displayHours <= 24) {  // 1天 -> 3天
        newDisplayHours = 72;
    } else if (displayHours <= 72) {  // 3天 -> 7天
        newDisplayHours = 168;
    } else if (displayHours <= 168) {  // 7天 -> 30天
        newDisplayHours = 720;
    } else if (displayHours < 720) {  // 其他值 -> 30天
        newDisplayHours = 720;
    } else {
        return;  // 已经是最大档位（30天），不能再缩小
    }

    displayHours = newDisplayHours;

    // 重新计算时间范围，保持中心点
    QDateTime centerTime = currentStartTime.addSecs(
        (currentEndTime.toSecsSinceEpoch() - currentStartTime.toSecsSinceEpoch()) / 2);
    currentStartTime = centerTime.addSecs(-(displayHours) * 3600 / 2);
    currentEndTime = centerTime.addSecs((displayHours) * 3600 / 2);

    // 确保不超出数据范围
    if (currentStartTime < dataStartTime) {
        currentStartTime = dataStartTime;
        currentEndTime = currentStartTime.addSecs((displayHours) * 3600);
    }
    if (currentEndTime > dataEndTime) {
        currentEndTime = dataEndTime;
        currentStartTime = currentEndTime.addSecs(-(displayHours) * 3600);
    }

    updateTimeRange();
}

void ChartWidget::onBackButtonClicked() { emit backToTableRequested(); }

void ChartWidget::onCrosshairMoved(const QPointF &chartPoint, const QPointF &actualDataPoint)
{
    if (!actualDataPoint.isNull()) {
        // 在固定标签中显示最接近的实际数据点信息
        QDateTime time = QDateTime::fromMSecsSinceEpoch(actualDataPoint.x());
        QString info = QString("时间: %1 | 值: %2")
                           .arg(time.toString("MM-dd hh:mm:ss"))
                           .arg(actualDataPoint.y(), 0, 'f', 2);

        dataInfoLabel->setText(info);
    } else {
        dataInfoLabel->setText("数据信息: 点击图表激活十字光标");
    }
}

void ChartWidget::applyThemeColors()
{
    // 获取当前应用的样式表
    QString styleSheet = qApp->styleSheet();

    // 根据样式表内容判断当前主题并设置相应颜色
    if (styleSheet.contains("#1e1e2e")) {
        // 现代深色主题 (Catppuccin)
        chart->setBackgroundBrush(QBrush(QColor("#1e1e2e")));
        chart->setTitleBrush(QBrush(QColor("#cdd6f4")));
        chart->legend()->setLabelColor(QColor("#cdd6f4"));
        chart->legend()->setColor(QColor("#cdd6f4"));
        chart->legend()->setBrush(QBrush(QColor("#313244")));

        // 更新坐标轴颜色
        if (axisX) {
            axisX->setLabelsColor(QColor("#cdd6f4"));
            axisX->setTitleBrush(QBrush(QColor("#cdd6f4")));
            axisX->setLinePenColor(QColor("#45475a"));
            axisX->setGridLineColor(QColor("#45475a"));
        }
        if (axisY) {
            axisY->setLabelsColor(QColor("#cdd6f4"));
            axisY->setTitleBrush(QBrush(QColor("#cdd6f4")));
            axisY->setLinePenColor(QColor("#45475a"));
            axisY->setGridLineColor(QColor("#45475a"));
        }

    } else if (styleSheet.contains("#0d1117")) {
        // 高端黑色主题 (GitHub Dark)
        chart->setBackgroundBrush(QBrush(QColor("#0d1117")));
        chart->setTitleBrush(QBrush(QColor("#f0f6fc")));
        chart->legend()->setLabelColor(QColor("#f0f6fc"));
        chart->legend()->setColor(QColor("#f0f6fc"));
        chart->legend()->setBrush(QBrush(QColor("#21262d")));

        // 更新坐标轴颜色
        if (axisX) {
            axisX->setLabelsColor(QColor("#f0f6fc"));
            axisX->setTitleBrush(QBrush(QColor("#f0f6fc")));
            axisX->setLinePenColor(QColor("#30363d"));
            axisX->setGridLineColor(QColor("#30363d"));
        }
        if (axisY) {
            axisY->setLabelsColor(QColor("#f0f6fc"));
            axisY->setTitleBrush(QBrush(QColor("#f0f6fc")));
            axisY->setLinePenColor(QColor("#30363d"));
            axisY->setGridLineColor(QColor("#30363d"));
        }

    } else {
        // 经典深色主题 (默认)
        chart->setBackgroundBrush(QBrush(QColor("#2c3e50")));
        chart->setTitleBrush(QBrush(QColor("#b1cdd4")));
        chart->legend()->setLabelColor(QColor("#b1cdd4"));
        chart->legend()->setColor(QColor("#b1cdd4"));
        chart->legend()->setBrush(QBrush(QColor("#34495e")));

        // 更新坐标轴颜色
        if (axisX) {
            axisX->setLabelsColor(QColor("#b1cdd4"));
            axisX->setTitleBrush(QBrush(QColor("#b1cdd4")));
            axisX->setLinePenColor(QColor("#6c7274"));
            axisX->setGridLineColor(QColor("#4a5b6f"));
        }
        if (axisY) {
            axisY->setLabelsColor(QColor("#b1cdd4"));
            axisY->setTitleBrush(QBrush(QColor("#b1cdd4")));
            axisY->setLinePenColor(QColor("#6c7274"));
            axisY->setGridLineColor(QColor("#4a5b6f"));
        }
    }
}